package com.ruoyi.mall.product.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.product.mapper.CategoryMapper;
import com.ruoyi.mall.product.domain.Category;
import com.ruoyi.mall.product.service.ICategoryService;

/**
 * 商品三级分类Service业务层处理
 * 
 * @author msb
 * @date 2024-09-10
 */
@Service
public class CategoryServiceImpl implements ICategoryService 
{
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询商品三级分类
     * 
     * @param catId 商品三级分类主键
     * @return 商品三级分类
     */
    @Override
    public Category selectCategoryByCatId(Long catId)
    {
        return categoryMapper.selectCategoryByCatId(catId);
    }

    /**
     * 查询商品三级分类列表
     * 
     * @param category 商品三级分类
     * @return 商品三级分类
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增商品三级分类
     * 
     * @param category 商品三级分类
     * @return 结果
     */
    @Override
    public int insertCategory(Category category)
    {
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改商品三级分类
     * 
     * @param category 商品三级分类
     * @return 结果
     */
    @Override
    public int updateCategory(Category category)
    {
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除商品三级分类
     * 
     * @param catIds 需要删除的商品三级分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCatIds(Long[] catIds)
    {
        return categoryMapper.deleteCategoryByCatIds(catIds);
    }

    /**
     * 删除商品三级分类信息
     * 
     * @param catId 商品三级分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCatId(Long catId)
    {
        return categoryMapper.deleteCategoryByCatId(catId);
    }

    @Override
    public List<Category> queryPageWithTree(Category params) {
        List<Category> categoryList = selectCategoryList(params);
        // parentCid是0的为第一级目录
        return categoryList.stream().filter(entity -> entity.getParentCid() == 0L)
                .peek(entity -> entity.setChildren(getCategoryChildrens(entity, categoryList)))
                .sorted((l1, l2) -> Math.toIntExact((l1.getSort() == null ? 0 : l1.getSort())
                        - (l2.getSort() == null ? 0 : l2.getSort()))).collect(Collectors.toList());
    }

    /**
     * 递归查询子列表
     * @param entity category
     * @param categoryList categoryList
     * @return list
     */
    private List<Category> getCategoryChildrens(Category entity, List<Category> categoryList) {
        return categoryList.stream()
                .filter(e -> Objects.equals(e.getParentCid(), entity.getCatId()))
                .peek(e -> e.setChildren(getCategoryChildrens(e, categoryList)))
                .sorted(((o1, o2) -> Math.toIntExact((o1.getSort() == null ? 0 : o1.getSort())
                        - (o2.getSort() == null ? 0 : o2.getSort())))).collect(Collectors.toList());
    }

}
